<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.model' );
	jimport( 'joomla.methods' );
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'implJoomla'.DS.'JDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'implJoomla'.DS.'JDAOEvent.php');
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'implFusionTables'.DS.'FTDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'DAOEvent.php');
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'implFusionTables'.DS.'FTDAOFactory.php');
	require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'implFusionTables'.DS.'FTDAOEvent.php');
	
	class ModelActinMapEvent extends JModel
	{
		
		function __construct()
		{
			parent::__construct();
		}

		public function getEventComments($id){
			$factoria=JDAOFactory::getInstance();
			$commentDao=$factoria->getDAOComment();
			return $commentDao->readComments($id);
		}
		
		public function setEventComments($dtoEventComment){
			$factoria=JDAOFactory::getInstance();
			$commentDao=$factoria->getDAOComment();
			return $commentDao->saveComment($dtoEventComment);
		}
		
		private function checkDTOPais($dtoPais){
			$daoPais=JDAOFactory::getInstance()->getDAOPais();
			$idPais=$daoPais->getPaisIdByName($dtoPais->getNamePais());
			if(!$idPais){
				$dtoPais=$daoPais->savePais($dtoPais);
			}
			else{
				$dtoPais->setIdPais($idPais);
			}
			return $dtoPais;
		}
		
		private function checkDTOAreaAdmin($dtoAreaAdmin){
			$daoAreaAdmin=JDAOFactory::getInstance()->getDAOAreaAdministrativa();
			$idAreaAdmin=$daoAreaAdmin->getAreaAdminIdByName($dtoAreaAdmin->getNameArea());
			if(!$idAreaAdmin){
				//Si la idAreaAdmin no existe para ese nombre, hay que guardar una nueva AreaAdmin
				//Y claro, el pais puede que tambien
				$dtoPais=$this->checkDTOPais($dtoAreaAdmin->getPais());
				
				$dtoAreaAdmin->setPais($dtoPais);
				$dtoAreaAdmin=$daoAreaAdmin->saveAreaAdmin($dtoAreaAdmin);
			}
			else{
				$dtoAreaAdmin->setIdArea($idAreaAdmin);
			}
			return $dtoAreaAdmin;
		}
		
		private function checkDTOEventArea($dtoEvent){
			//TODO me pueden estar metiendo una id que no existe, nos fiamos de la ID
			$dtoAreaAdmin=$dtoEvent->getAreaAdmin();
			if(!$dtoAreaAdmin->getIdArea()){
				//Si el DTOAreaAdmin no tiene la id seteada, nuestra mision es setearla
				$dtoAreaAdmin=$this->checkDTOAreaAdmin($dtoAreaAdmin);
				$dtoEvent->setAreaAdmin($dtoAreaAdmin);
			}
			return $dtoEvent;
		}
		
		public function saveEvent($dtoEvent){
			//guardamos en local
			$dtoEvent=$this->checkDTOEventArea($dtoEvent);
			$eventDao=JDAOFactory::getInstance()->getDAOEvent();	
			$eventDto=$eventDao->saveEvent($dtoEvent);
			
			//guardamos en la FT de google
			$daoEventFT=FTDAOFactory::getInstance()->getDAOEvent();
			$daoEventFT->saveEvent($eventDto);
			return $eventDto;
		}
		
		public function updateEvent($DtoEvent){
			//Setear el nombre del AreaAdmin
			
			$daoAreaAdmin=JDAOFactory::getInstance()->getDAOAreaAdministrativa();
			$nombreArea=$daoAreaAdmin->getNameByAreaAdminId($DtoEvent->getAreaAdmin()->getIdArea());		
			$DtoEvent->getAreaAdmin()->setNameArea($nombreArea);
			//guardamos en local
			$factoriaDto=DTOFactory::getInstance();
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();		
			$eventDto=$eventDao->saveEvent($DtoEvent);
			
			
			//guardamos en la FT de google
			$factoriaFT=FTDAOFactory::getInstance();
			$daoEventFT=$factoriaFT->getDAOEvent();
			$daoEventFT->updateEvent($eventDto);
			return $eventDto;
		}
		
		public function getEvents(){
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();
			return $eventDao->readEvents();
		}
		
		public function getEventViews($idEvent){
			$eventDao=JDAOFactory::getInstance()->getDAOEvent();
			return $eventDao->getEventViews($idEvent);
		}
			
		public function addEventView($idEvent){
			$eventDao=JDAOFactory::getInstance()->getDAOEvent();
			$ok=$eventDao->addEventView($idEvent);
			if($ok){
				$eventFTDao=FTDAOFactory::getInstance()->getDAOEvent();
				$ok=$eventFTDao->addEventView($idEvent);
			}
			return $ok;
		}
		
		public function getUserEvents($idUser)
		{
		    //obtencion de lista de eventos creada por idUser
			$arrayCreados=$this->getUserCreatedEvents($idUser);
			//obtencion de lista de eventos suscrito por idUser
			$arraySuscritos=$this->getUserSubscribedEvents($idUser);
			//juntar los eventos
			$result = array_merge((array)$arrayCreados, (array)$arraySuscritos);
			return $result;
		}

		public function suscribeEvent($eventSuscrDto){
			$eventDao=JDAOFactory::getInstance()->getDAOEventSuscription();
			return $eventDao->suscribeEvent($eventSuscrDto);
		}

		private function getUserCreatedEvents($idUser)
		{
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();
			return $eventDao->readUserCreatedEvents($idUser);
		}

		private function getUserSubscribedEvents($idUser)
		{
			$factoria=JDAOFactory::getInstance();
			$suscriptionDao=$factoria->getDAOEventSuscription();
			return $suscriptionDao->readUserSuscribeEvents($idUser);

		}
		
		public function isUserSubscribed($idEvent,$idUser){
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEventSuscription();
			return $eventDao->checkEventSuscription($idEvent, $idUser);
		}
		
		public function isUserCreator($idEvent,$idCreador){
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();	
			return $eventDao->checkEventCreator($idEvent, $idCreador);//true o false
		}
		
		public function deleteEventSubscription($idEvent,$idUser){
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEventSuscription();
			$eventDao->deleteEventSubscription($idEvent,$idUser);
		}
				
		public function deleteEvent($idEvent){
			
			//borrar en local
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();
			$eventDao->deleteEvent($idEvent);
			
			//borrar suscripciones
			$suscriptionsEvent=$factoria->getDAOEventSuscription();
			$suscriptionsEvent->deleteEventSubscriptions($idEvent);
			
			//borrar en ftable
			$factoriaFT=FTDAOFactory::getInstance();
			$daoEventFT=$factoriaFT->getDAOEvent();
			$daoEventFT->deleteEvent($idEvent);
			
		}
		
		public function isEvent($idEvent){
			$factoria=JDAOFactory::getInstance();
			$eventDao=$factoria->getDAOEvent();
			return $eventDao->readEventById($idEvent);		
		}	
		public function getEventCategories(){
			$factoria=JDAOFactory::getInstance();
			$categoryDao=$eventDao=$factoria->getDAOCategory();
			return $categoryDao->getCategories();
		}
	}
